const mongoose = require('mongoose')
const Model = require('../model/power/roleRight')
const Right = require('../model/power/right')
const { Role, Rightance, SecondRight, ThreeRight } = Model

const roleRightController = {
    async createMulit(req,res) {
        const body = req.query
        let ThreeRight1 = new ThreeRight({
        _id: new mongoose.Types.ObjectId(),
        threeName: body.threeName
      })
      try {
        await ThreeRight.findOne({}).exec((err,doc)=>{
            ThreeRight1.save()
             let SecondRight1 = new SecondRight({
                _id: new mongoose.Types.ObjectId(),
                secondName: body.secondName,
                threeRights:ThreeRight1._id
              })
              SecondRight1.save()
              let Right1 = new Rightance({
                _id: new mongoose.Types.ObjectId(),
                rightName: body.rightName,
                secondRights:SecondRight1._id
              })
              Right1.save()
              let Role1 = new Role({
                _id: new mongoose.Types.ObjectId(),
                roleName:body.roleName,
                roleDesc:body.roleDesc,
                rights:Right1._id
              })
              Role1.save()
              res.status(200).json(doc)
        })
      } catch (error) {
       res.status(500).json({
         error_code: 1,
         message: '服务器错误'
       })
      }
    },
    async addThreeRight(req,res) {
      const body = req.query
      var ThreeRight1 = new ThreeRight({
          _id: new mongoose.Types.ObjectId(),
          threeName: body.threeName
      })
      try {
          await ThreeRight.findOne({}).exec((err,doc)=>{

            ThreeRight1.save()  
            SecondRight.updateOne({'_id':"5e636a9372d10c266006affa"},{$push:{threeRights:ThreeRight1._id}}).exec((err,doc)=>{
                  res.status(200).json(doc)
              })
          })

        } catch (error) {
         res.status(500).json({
           error_code: 1,
           message: '服务器错误'
         })
        }
    },
    async addSecondRight(req,res) {
      const body = req.query
      var SecondRight1 = new SecondRight({
          _id: new mongoose.Types.ObjectId(),
          secondName: body.secondName
      })
      try {
          await SecondRight.findOne({}).exec((err,doc)=>{

            SecondRight1.save()  
            Rightance.updateOne({'_id':"5e63696d8c7a5931a820d533"},{$push:{secondRights:SecondRight1._id}}).exec((err,doc)=>{
                  res.status(200).json(doc)
              })
          })

        } catch (error) {
         res.status(500).json({
           error_code: 1,
           message: '服务器错误'
         })
        }
    },
    async addRightance(req,res) {
      const body = req.query
      var Rightance1 = new Rightance({
          _id: new mongoose.Types.ObjectId(),
          rightName: body.rightName
      })
      try {
          await Rightance.findOne({}).exec((err,doc)=>{

            Rightance1.save()  
            Role.updateOne({'_id':"5e635fca5e4fc832e0311563"},{$push:{rights:Rightance1._id}}).exec((err,doc)=>{
                  res.status(200).json(doc)
              })
          })

        } catch (error) {
         res.status(500).json({
           error_code: 1,
           message: '服务器错误'
         })
        }
    },
    async deleteSecondRight(req,res) {
      let _id = req.query._id
      console.log(_id)
      try {
        await SecondRight.deleteOne({_id : _id}).exec((err,result) =>{
          if(result){
            return res.status(200).json({
              message: '删除成功！'
            })
          }
          res.status(201).json({
            message: '删除失败！'
          })
        })     
      } catch (error) {
          res.status(500).json({
              error_code: 1,
              message: '服务器错误'
        })
      }
    },
    async deleteThreeRight(req,res) {
      let _id = req.query.roleId
      let id = req.query.rightId
      console.log(id)
      console.log(_id)
      try {
        await ThreeRight.findOne({_id : _id}).remove({_id:id}).exec((err,result) =>{
          
          if(result){
            return  res.status(200).json({
                   message: '删除成功！'
            })
          }
          res.status(201).json({
            message: '删除失败！'
          })
        })
  
      } catch (error) {
          res.status(500).json({
              error_code: 1,
              message: '服务器错误'
        })
      }
    },
    async deleteRole(req,res) {
      let _id = req.query._id
      console.log(_id)
      try {
        await Role.deleteOne({_id : _id}).exec((err,result) =>{
          if(result){
            return res.status(200).json({
              message: '删除成功！'
            })
          }
          res.status(201).json({
            message: '删除失败！'
          })
        })     
      } catch (error) {
          res.status(500).json({
              error_code: 1,
              message: '服务器错误'
        })
      }
    },
    async getRoleRight(req,res) {
      try {
        await Role.find().populate({
          path: 'rights',
          select: '_id rightName',
          model: 'Rightance',
          populate: {
              path: 'secondRights',
              select: '_id secondName',
              model: 'SecondRight',
              populate: {
                path: 'threeRights',
                select: '_id threeName',
                model: 'ThreeRight'
              }
          }
      }).exec((err,result)=>{
          console.log(Role)
          return res.status(200).json(result)
        })
      } catch (error) {
        res.status(500).json({
          error_code: 1,
          message: '服务器错误'
        })
      }
    },
    async getAllRight(req,res) {
      try {
        await Rightance.find().populate({
          path: 'secondRights',
          select: '_id secondName',
          model: 'SecondRight',
          populate: {
              path: 'threeRights',
              select: '_id threeName',
              model: 'ThreeRight'
          }
      }).exec((err,result)=>{
          return res.status(200).json(result)
        })
      } catch (error) {
        res.status(500).json({
          error_code: 1,
          message: '服务器错误'
        })
      }
    },
    async dispatchRight(req,res) {
      const body = req.body
      let roleId = body.roleId
      let right = (body.right).toString()
      console.log(roleId)
      console.log(right)
      try {
        const doc = await Right.findOne({'children.children.id':right})  
        await Role.updateOne({_id:roleId},{$push:{threeRights:doc.children[0].children[1].authName}}).exec((err,result)=>{
          res.status(200).json({
            doc,
            result
          })
        })
      } catch (error) {
       res.status(500).json({
         error_code: 1,
         message: '服务器错误'
       })
      }
    },
    async getRoleList(req,res) {
      try {
        await Role.find({}).exec((err,doc)=>{
          res.status(200).json(doc)
        })
      } catch (error) {
        res.status(500).json({
          error_code: 1,
          message: '服务器错误'
        })
      }
    }
    
    
  
}

process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection:', reason)
  // 在这里处理
})
module.exports = roleRightController